import {rental} from './proto_gen/rental/rental_pb'
import {RentalCar} from './request'

export namespace TripService {

    // 创建行程
    export function CreateTrip(req: rental.v1.ICreateTripRequest): Promise<rental.v1.ITripEntity> {
        return RentalCar.sendRequestWithAuthRetry({
                method: 'POST',
                path: '/v1/trip',
                data: req,
                respMarshaller: rental.v1.TripEntity.fromObject
            }
        )
    }

    // 根据ID获取行程
    export function GetTrip(id: string): Promise<rental.v1.ITrip> {
        return RentalCar.sendRequestWithAuthRetry({
            method: 'GET',
            path: `/v1/trip/${encodeURIComponent(id)}`,
            respMarshaller: rental.v1.Trip.fromObject
        })
    }

    // 根据状态获取行程
    export function GetTrips(status?: rental.v1.TripStatus): Promise<rental.v1.IGetTripsResponse> {

        let path = `/v1/trips`

        if (status) {
            path += `?status=${status}`
        }

        return RentalCar.sendRequestWithAuthRetry({
            method: 'GET',
            path: path,
            respMarshaller: rental.v1.GetTripsResponse.fromObject
        })

    }

}